package msgCenter;

import org.json.JSONException;
import org.json.JSONObject;

import com.netease.util.Util;

public class PushMsgParse {
	public static int FEATURE_MESSAGE = 1;
	public static int FEATURE_MAIL = 2;
	public static int FEATURE_LOCAL_MESSAGE = 3;
	/*
	 * push类型及都有的字段
	 */
	public int feature;
	public String msgId;  //push 服务器产生的ID
	public String msg;	  //推送消息内容
	public int msgType;   //推送消息类型 详见XoneConsts.PUSHMSG_TYPE
	public long rtime;
	
	/*
	 * message push
	 * {"msgId":1371700355153,"type":4,"msg":"您有1条新的评论"}
	 */
	
	public static PushMsgParse parseJson(String json) {
		if (Util.isStringEmpty(json))
			return null;

		PushMsgParse parse = new PushMsgParse();
		try {
			JSONObject jso = new JSONObject(json);
			parse.feature = FEATURE_MESSAGE;
			parse.msgId = jso.optString("msgId");
			parse.msg = jso.optString("msg");
			parse.msgType = jso.optInt("type");
			
			//TODO 等协议加上后再补充，默认先用系统时间
			parse.rtime = jso.optLong("uTime");
			
			if (parse.rtime <= 0) {
				parse.rtime = System.currentTimeMillis();
			}

			return parse;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@Override
	public String toString() {
		JSONObject jso = new JSONObject();
		try {
			jso.put("msgId", msgId);
			jso.put("msg", msg);
			jso.put("type", msgType);
			jso.put("uTime", rtime);
			jso.put("feature", "message");
		} catch (JSONException e) {
			e.printStackTrace();
		}	
		return jso.toString();
	}
}
